<?php
 
namespace App\Http\Controllers;

use DB;
use App\Cpeizhi; 

use App\Datatables;
use Illuminate\Http\Request;
use Storage;

class PunchController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 配置列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function peizhiList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::peizhiList());
        }
        return view('punch.peizhilist');
    }

    /**
     * 添加配置
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function peizhiPublish(Request $request)
    {

        if ($request->isMethod('post')) {
            $return = Cpeizhi::peizhiPublish($request);
            if ($return['status'] === 1) {
                return redirect('/punch/list')->with('status', $return['status'])->with('message', $return['message']);
            } else {
                return back()->with('status', $return['status'])->with('message', $return['message']);
            }
        }

        return view('punch.peizhiPublish');
    }


    /**
     * 修改配置
     * @param Request $request
     * @param fenlei $fenlei
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function peizhiModify(Request $request, Cpeizhi $Cpeizhi)
    {
        if ($request->isMethod('post')) {
            $return = Cpeizhi::peizhiModify($request, $Cpeizhi);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
				
        return view('punch.peizhiModify')->with(compact('Cpeizhi'));
    }

    /**
     * 删除配置
     * @param fenlei $fenlei
     * @return array
     * @throws \Exception 
     */
    public function peizhiiDels($id, Request $request)
    {
		$fenlei = Cpeizhi::where('id', $id)->exists();
        if (!$fenlei) return ['status' => 2, 'message' => '无此配置项'];
        return Cpeizhi::Dels($id, $request);
    }
	
	//配置开启
	public function pzstatuson($id)
    {
        $advice = Cpeizhi::find($id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->status = Cpeizhi::STATUS_YES;
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }
	
	//配置关闭
	public function pzstatusoff($id){
        $advice = Cpeizhi::find($id);
        if (!$advice) { 
            return ['status' => 2, 'message' => '出错了'];
        } 
        $advice->status = Cpeizhi::STATUS_NO;
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];	
	}
	
	//打卡记录列表
    public function peizhirecode($id, Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::recodeList($id));
        }
        return view('punch.recode'); 
    }
	//统计
	public function peizhitongji(Request $request)
	{
		if($request->isMethod('post')) {
            return response()->json(Datatables::tongjiList());
        }
        return view('punch.tongji');
		
	}

}
